package com.jucai.pppeiyouclient.weidget.photo;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.utils.ScreenUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 照片墙适配器
 * Created by Administrator on 2017/1/1.
 */
public class AllPictureAdapter extends AllPictureCommonAdapter<String> {
    private Activity activity;
    private String myPath = "";
    private ArrayList<String> pathListTemp = new ArrayList<String>();
    private final int MAX_COUNT = 4;
    private int width = 0;

    public AllPictureAdapter(Activity activity, List<String> mDatas, int itemLayoutId, String path, ArrayList<String> pathListTemp) {
        super(activity, mDatas, itemLayoutId);
        this.activity = activity;
        this.myPath = path;
        this.pathListTemp = pathListTemp;
        width = (ScreenUtil.getScreenWidth(activity) - ScreenUtil.dip2px(activity, 20)) / 3;
    }

    @Override
    public void convert(AllPictureViewHolder helper, final String item, int position) {
        helper.setImageResource(R.id.givPic, R.drawable.all_pictures_no);
        helper.setImageResource(R.id.isSelect, R.drawable.all_picture_unselected);

        final ImageView givPic = helper.getView(R.id.givPic);
        givPic.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, width));
        final ImageView isSelect = helper.getView(R.id.isSelect);

        if (TextUtils.isEmpty(myPath)) {
            Glide.with(activity).load("file://" + item).into(givPic);
        } else {
            Glide.with(activity).load("file://" + myPath + "/" + item).into(givPic);
        }
        givPic.setColorFilter(null);
        givPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.equals(myPath,"")) {
                    if (pathListTemp.contains(item)) {
                        pathListTemp.remove(item);
                        givPic.setColorFilter(null);
                        isSelect.setImageResource(R.drawable.all_picture_unselected);
                    } else {
                        if (pathListTemp.size() >= MAX_COUNT) {
                            ToastUtils.showToast("最多只能上传" + MAX_COUNT + "张图片哦");
                        } else {
                            pathListTemp.add(item);
                            givPic.setColorFilter(Color.parseColor("#77000000"));
                            isSelect.setImageResource(R.drawable.all_picture_selected);
                        }
                    }
                } else {
                    if (pathListTemp.contains(myPath + "/" + item)) {
                        pathListTemp.remove(myPath + "/" + item);
                        givPic.setColorFilter(null);
                        isSelect.setImageResource(R.drawable.all_picture_unselected);
                    } else {
                        if (pathListTemp.size() >= MAX_COUNT) {
                            ToastUtils.showToast("最多只能上传" + MAX_COUNT + "张图片哦");
                        } else {
                            pathListTemp.add(myPath + "/" + item);
                            givPic.setColorFilter(Color.parseColor("#77000000"));
                            isSelect.setImageResource(R.drawable.all_picture_selected);
                        }
                    }
                }
            }
        });
        if (pathListTemp.contains(item) || pathListTemp.contains(myPath + "/" + item)) {
            givPic.setColorFilter(Color.parseColor("#77000000"));
            isSelect.setImageResource(R.drawable.all_picture_selected);
        }
    }

}